package com.imooc.netty.websocket;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2020/12/28 22:21
 */
public class WSServer
{
    public static void main(String[] args)
    {
        //主子线程组
        NioEventLoopGroup mainGroup = new NioEventLoopGroup();
        NioEventLoopGroup subGroup = new NioEventLoopGroup();
        ServerBootstrap serverBootstrap = null;
        try
        {
            serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(mainGroup, subGroup)//设置主从线程组
                    .channel(NioServerSocketChannel.class)//设置nio双向通道
                    .childHandler(new WSServerInitializer());//子处理器,用于处理workerGroup
            ChannelFuture future = serverBootstrap.bind(8088).sync();
            //监听关闭的channel,设置为同步方式
            future.channel().closeFuture().sync();
        } catch (Exception e)
        {
            e.printStackTrace();
        } finally
        {
            //优雅关闭
            mainGroup.shutdownGracefully();
            subGroup.shutdownGracefully();
        }
    }
}
